<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
 <script type="text/javascript" src="../js/jquery-2.1.1.min.js"></script>
        <link rel="stylesheet" href="../js/layui/css/layui.css">
        <script type="text/javascript" src="../js/layui/layui.js"></script>
<!-- 
<style type="text/css">
	.layui-tree li .layui-tree-check:hover{
	color:block
	}
</style> -->

        <script type="text/javascript">
       
        
        $(function (){
        	getModulesTree();
        	 
        	
        })
        var layer;
        var mid;
        var mname;
        var parentId;
        var path;
        var weight;
        function getModulesTree(){
        	
        		 $.ajax({
   				  type:"post",
   				  url:"getTreeModules",
   				  data:{},
   				  dataType:"json",
   				  success:function(res){
   					  layui.use(["tree","form","layer"],function(){
   						  var form = layui.form;
   						   var tree=layui.tree;
   						   layer=layui.layer;
   						   
   						    layui.tree({
   						    //title:"点击模块进行操纵"
       						  elem:".modilesul",
       						  form: form,
       						  nodes:res,
       						  click:function(nodes){
       							  
       							   mid=nodes.id;
       							   mname=nodes.name;
       							   path=nodes.path;
       							   parentId=nodes.parentId;
       							   weight=nodes.weight;
       						/* 	   layer.open({
       								   title :'操作'，
       								   area:['60px','20px'],
       								   type:1,
       								   content:$("#edddd")
       							   }) */
       							layer.open({
       							  type:1,
       							  title:"操作",
       							  area:['300px','120px'],
       		        		      content:$("#edddd")
       						  })	
       						  }
       						  
       				           
       					  }) 
       					  
   					  }) 
   					 
   				  }
   			 
        	})
        }
        function openAddModulesBtn(){
        	
        	if(mid==undefined){
        		
        		alert("请选择要添加的父节点");
        		return;
        	}else{
        		
        		layer.open({
					  type:1,
					  title:"即将给"+mname+"添加子节点",
        		      content:$("#addModulesDiv")
				  })	
        	}
        	
				  
			  }
        
       //点击添加保存
        function saveAddModulesBtn(){
    	  var name=$("#mname").val();
    	  var path=$("#murl").val();
    	  var weight=$("#mweight").val();
    	  layer.confirm("确认添加吗",function (){
        		$.ajax({
        			url:"insertModule",
        			type:"post",
        			data:{
        				name:name,
        				path:path,
        				weight:weight,
        				parentId:mid
        			},
        			dataType:"json",
        			success:function(res){
        				if(res.success){
        					alert(res.message);
        					
        					layer.closeAll();
        					window.location.reload();
        				}else{
        					alert(res.message);
        				}
        			}
        		})
        	})
        }
        
       
       function openupdateModulesBtn(){
    	   if(mid==undefined){
       		
       		alert("请选择要修改的节点");
       		return;
       	}else{
       	
       		$("#uname").val(mname);
       		$("#uid").val(mid);
       		$("#uweight").val(weight);
       		$("#uurl").val(path);
       		layer.open({
					  type:1,
					  title:"即将给"+mname+"修改节点",
       		      content:$("#updateModulesDiv")
				  })	
       	}
       }
       function saveUpdateModulesBtn(){
    	   var id=$("#uid").val();
    	   var name=$("#uname").val();
      		var weight=$("#uweight").val();
      		var paht=$("#uurl").val(); 
      		layer.confirm("确认修改吗",function (){
    		   $.ajax({
    			   url:"updatetModule",
    			   type:"post",
    			   data:{
    				   name:name,
    				   weight:weight,
    				   paht:paht,
    				   id:id
    			   },
    			   dataType:"json",
    			   success:function(res){
    				   if(res.success){
    					   alert(res.message);
    					   layer.closeAll();
    					   window.location.reload();
    				   }else{
    					   alert(res.message);
    					   
    				   }
    			   }
    		   })
    	   })
       }
       
       //点击删除
       function opendelModulesBtn(){
    	   if(mid==undefined){
          		
          		alert("请选择要删除的节点");
          		return;
          	}else{
          		//alert(mid)
          	    layer.confirm("确认删除吗",function (){
          		   
    
          	    	$.ajax({
          	    		
          	    		type:"post",
          	    		url:"delModule",
          	    		data:{
          	    			moduleId:mid
          	    		},
          	    		dataType:"json",
          	    		success:function(res){
          	    			if(res.success){
          	    				alert(res.message);
          	    				window.location.reload();
          	    			}else{
          	    				alert(res.message);
          	    			}
          	    		}
          	    	})
          	    })
          	}
       }
        </script>
        <script type="text/javascript">
        //验证添加名称
        function checkAddModulesName(){
        	
        	 var name=$("#mname").val();
        	 $.ajax({
        		 type:"post",
        		 url:"checkModulesName",
        		 data:{
        			 parentId:mid,
        			 name:name
        		 },
        		 dataType:"json",
        		 success:function(res){
        			 if(res.success){
        				 $("#mtxt1").html(res.message)
        			 }else{
        				 $("#mtxt1").html(res.message)
        			 }
        		 }
        		 
        	 })
        }
        //验证权重只能为数字
        function checkAddModulesQZ(){
        	
        	  var weight=$("#mweight").val();
        	
        	var reg = /^[0-9]*$/;
    		if(reg.test(weight)){
    			$("#mtxt2").html("权重输入正确");
    			return  true;
    			
    		}else{
    			$("#mtxt2").html("权重只能是数字");
    			return false;
    		}
        }
       //修改时验证名称
       function checkupdateModulesName(){
    	   var name=$("#uname").val();
    	  /*  alert(name)
    	   alert(parentId) */
      	 $.ajax({
      		 type:"post",
      		 url:"checkModulesName",
      		 data:{
      			 parentId:parentId,
      			 name:name
      		 },
      		 dataType:"json",
      		 success:function(res){
      			 if(res.success){
      				 $("#mutxt1").html(res.message)
      			 }else{
      				 $("#mutxt1").html(res.message)
      			 }
      		 }
      		 
      	 })
       }
       
       //修改时验证权重
       function checkupdateModulesQZ(){
    	   var weight=$("#uweight").val();
       	alert(weight)
       	var reg = /^[0-9]*$/;
   		if(reg.test(weight)){
   			$("#mutxt2").html("权重输入正确");
   			return  true;
   			
   		}else{
   			$("#mutxt2").html("权重只能是数字");
   			return false;
   		}
       }
        </script>
</head>
<body>
<div>
	<h3>点击模块进行操作</h3>
</div>
<div id="edddd" style="margin-left: 40px;margin-top: 10px; display: none;">
<input class="layui-btn" type="button" value="添加" onclick="openAddModulesBtn()"></input>
<input class="layui-btn" type="button" value="修改" onclick="openupdateModulesBtn()"></input>
<input class="layui-btn" type="button" value="删除" onclick="opendelModulesBtn()"></input>
</div>

<div >

<ul class="modilesul" style="margin-left: 100px;margin-top: 30px;">

</ul>

</div>
<!-- 添加子节点 -->
<div id="addModulesDiv" style="display:none;margin-top: 50px;">
<form action="" class="layui-form">
<div class="layui-form-item">
    <label class="layui-form-label">节点名称</label>
    <div class="layui-input-inline">
      <input type="text" id="mname" onblur="checkAddModulesName()" lay-verify="title" autocomplete="off" placeholder="请输入用户名" class="layui-input">
    </div>
    <span id="mtxt1" style="margin-left: 120px;"></span>
    </div>
    <div class="layui-form-item">
    <label class="layui-form-label">路径</label>
    <div class="layui-input-inline">
      <input type="text" id="murl" lay-verify="title" autocomplete="off" placeholder="请输入路径" class="layui-input">
    </div>
  
    </div>
    <div class="layui-form-item">
    <label class="layui-form-label">权重</label>
    <div class="layui-input-inline">
      <input type="text" id="mweight" onblur="checkAddModulesQZ()" lay-verify="title" autocomplete="off" placeholder="请输入权重" class="layui-input">
    </div>
   <span id="mtxt2" style="margin-left: 120px;"></span>
    </div>
    
     <div class="layui-form-item">
    <div class="layui-input-block">
        <input class="layui-btn" type="button" value="提交" onclick="saveAddModulesBtn()"></input>
       <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
 
</form>
</div>
<!--修改子节点 -->
<div id="updateModulesDiv" style="display:none;margin-top: 50px;">
<form action="" class="layui-form">
<input type="hidden" id="uid" name="id"/>
<div class="layui-form-item">
    <label class="layui-form-label">节点名称</label>
    <div class="layui-input-inline">
      <input type="text" name="name" id="uname" onblur="checkupdateModulesName()" lay-verify="title" class="layui-input">
    </div>
   <span id="mutxt1" style="margin-left: 120px;"></span>
    </div>
    <div class="layui-form-item">
    <label class="layui-form-label">路径</label>
    <div class="layui-input-inline">
      <input type="text" name="path" id="uurl"  lay-verify="title"  class="layui-input">
    </div>
   
    </div>
    <div class="layui-form-item">
    <label class="layui-form-label">权重</label>
    <div class="layui-input-inline">
      <input type="text" name="weight" id="uweight" onblur="checkupdateModulesQZ()" lay-verify="title"  class="layui-input">
    </div>
   <span id="mutxt2" style="margin-left: 120px;"></span>
    </div>
    
     <div class="layui-form-item">
    <div class="layui-input-block">
        <input class="layui-btn" type="button" value="提交" onclick="saveUpdateModulesBtn()"></input>
       <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
 
</form>
</div>
<!-- 
var reg = /^[0-9]*$/;
          		    	if(treeNode.weight !="" && reg.test(treeNode.weight)){
          	    			layer.msg("權重不能爲空且只能為數字");
          	    			return;
          		    	}else{
          		      	$.ajax({
          		        type: "post",
          		        data: {
          		        	//id:treeNode.nodeId,
          		        	name:treeNode.context,
          		        	//ops:treeNode.details,
          		        	weight:treeNode.weight,
          		        	path:treeNode.url,
          		        	parentId:treeNode.parentId
          		        },
          		        url: "insertModule",
          		        success: function(result){
          		          //DTree1.changeTreeNodeAdd(treeNode.nodeId); // 添加成功，返回ID
          		          	//DemoTree.changeTreeNodeAdd(true); // 添加成功
          		          //DTree1.changeTreeNodeAdd(result.data); // 添加成功，返回一个JSON对象
          		          if(result.success){
          		        	  layer.msg(result.message);
          		        		DemoTree.changeTreeNodeAdd("refresh"); // 添加成功，局部刷新树
          		          }else{
          		        	DemoTree.changeTreeNodeAdd("refresh"); // 添加成功，局部刷新树
          		          }
          		          //	DemoTree.changeTreeNodeAdd("refresh"); // 添加成功，局部刷新树
          		        },
          		       /*  error: function(){
          		        	DemoTree.changeTreeNodeAdd(false); // 添加失败
          		        } */
          		      }); 
          		    	} -->
</body>
</html>